from typing import Any

from aiogram import F, types
from aiogram_dialog import DialogManager, Window
from aiogram_dialog.widgets.kbd import Back, Button, Group

from app.bot.dialog.dialog_states import OrderDetailsSG
from app.bot.dialog.getters.orders import extend_summary_getter, order_getter
from app.bot.dialog.widgets import LocaleText
from app.bot.services.helpers import get_expiring_on
from app.bot.services.locale import Locale
from app.bot.services.loggers import logger
from app.database.services.orders.common import get_order
from app.database.services.orders.proxy_orders import update_order_expiring
from app.database.services.users import get_user


async def extend_order(call: types.CallbackQuery, widget: Any, manager: DialogManager):

    db_session = manager.middleware_data.get("db_session")
    locale: Locale = manager.middleware_data.get("locale")
    order_id: int = manager.dialog_data.get("order_id")
    period: str = manager.dialog_data.get("period")
    extend_price: float = manager.dialog_data.get("extend_price")

    user_id = call.from_user.id
    user = await get_user(db_session, user_id)

    if user.balance < extend_price:
        await call.answer(locale.get("not-enough-balance", balance=user.balance))
        return

    try:
        order = await get_order(db_session, order_id)
        extended_expiring_on = await get_expiring_on(order.expiring_on, period)

        await update_order_expiring(
            db_session, order_id, extended_expiring_on, extend_price, user_id
        )

        await call.answer(locale.get("order-extended-success"))
        await manager.switch_to(OrderDetailsSG.order_details)

    except Exception as e:
        logger.exception(e)
        await call.answer(locale.get("error-msg"))


async def start_order_extend(
    call: types.CallbackQuery, widget: Any, manager: DialogManager
):
    await manager.switch_to(OrderDetailsSG.extend_order)


async def set_proxy_period(
    call: types.CallbackQuery, widget: Any, manager: DialogManager
):

    manager.dialog_data["period"] = widget.widget_id
    await manager.switch_to(OrderDetailsSG.extend_confirmation)


extend_order_window = Window(
    LocaleText("extend-period"),
    Group(
        Button(
            LocaleText("four_hours"),
            id="four_hours",
            on_click=set_proxy_period,
            when=F["order_type"] == "proxy",
        ),
        Button(
            LocaleText("one_day"),
            id="one_day",
            on_click=set_proxy_period,
            when=F["order_type"] != "vpn",
        ),
        Button(
            LocaleText("one_week"),
            id="one_week",
            on_click=set_proxy_period,
            when=F["order_type"] == "proxy",
        ),
        Button(
            LocaleText("two_weeks"),
            id="two_weeks",
            on_click=set_proxy_period,
            when=F["order_type"] != "vpn",
        ),
        Button(
            LocaleText("one_month"),
            id="one_month",
            on_click=set_proxy_period,
        ),
        width=2,
    ),
    Back(LocaleText("back-btn")),
    state=OrderDetailsSG.extend_order,
    getter=order_getter,
)

extend_confirmation_window = Window(
    LocaleText("order-overview"),
    LocaleText("expiring-on", expiring_on="{expiring_on}"),
    LocaleText("extended-expiring-on", extended_expiring_on="{extended_expiring_on}"),
    LocaleText("extend-order-price", extend_price="{extend_price}$"),
    Button(
        LocaleText("confirm-btn"),
        id="confirm_order_button",
        on_click=extend_order,
    ),
    Back(LocaleText("back-btn")),
    state=OrderDetailsSG.extend_confirmation,
    getter=extend_summary_getter,
)
